<?php
session_start();
require('class/auth.php');
include_once('class/autoload.php'); // pour inclure nos classes

if(Auth::islog()){
}else{
	header('Location:index.php');
}
$mail=$_GET['mail'];
$modifcoordonneeseleve = new page_eleve($mail);

$site = new page_securiser('acceuil');


$site->titre = "Appartement";
$site->corps='<input type="button" value="Coordonn&eacute;es" onclick="location.href=\'coordonneeseleve.php?mail='.$mail.'\';">';




$site->affiche();
if(!empty($_POST))
{
	/*
	$nom = addslashes($_POST['nom']);
	$prenom = addslashes($_POST['prenom']);
	$tel =  addslashes($_POST['tel']);
	
	$u = array('nom'=>$nom,'prenom'=>$prenom, 'tel'=>$tel, 'email'=>$mail);
	$sql = 'UPDATE eleve SET nom = :nom, prenom = :prenom, tel = :tel WHERE mail = :email';
	$req = $connexion->prepare($sql);
	$req->execute($u);
	
	header('Location:coordonneeseleve.php?mail='.$mail.'');
	*/
	
	$allowedExts = array("jpeg", "jpg");
	$temp = explode(".", $_FILES["file"]["name"]);
	$extension = end($temp);
	if (is_uploaded_file($_FILES['file']['tmp_name']))
	{
		if (($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") && ($_FILES["file"]["size"] < 200000) && in_array($extension, $allowedExts))
		{
			if ($_FILES["file"]["error"] > 0)
			{
				echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
		    }
			else
		    {
				//echo "Upload: " . $_FILES["file"]["name"] . "<br>";
			 	//echo "Type: " . $_FILES["file"]["type"] . "<br>";
			    //echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
			    //echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
			
			    //if (file_exists( $_SERVER['HTTP_ORIGIN']."/images/avatar_eleve/" . $_FILES["file"]["name"]))
			    // {
			   		// echo $_FILES["file"]["name"] . " already exists. ";
			    //}
			    //else
			    //{
			    	$requete = 'SELECT id FROM eleve WHERE mail = "'.$mail.'";';
			    	$result = $connexion->query($requete);
			    	$result->setFetchMode(PDO::FETCH_OBJ);
			    	while($data = $result->fetch()) {
			    		$id = $data->id;
			    	}
			    	$result->closeCursor();
			    	
			    	if (file_exists("images/avatar_eleve/".$id."_avatar.".pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION)))
			    	{
			    		unlink("images/avatar_eleve/".$id."_avatar.".pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION));
			    	}
			    	move_uploaded_file($_FILES["file"]["tmp_name"],"images/avatar_eleve/" . $id."_avatar.".pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION));
				    $id_avatar = $id."_avatar";
				    $nom = addslashes($_POST['nom']);
				    $prenom = addslashes($_POST['prenom']);
				    $tel =  addslashes($_POST['tel']);
				      
				    $u = array('nom'=>$nom,'prenom'=>$prenom, 'tel'=>$tel, 'id_avatar'=>$id_avatar, 'email'=>$mail);
				    $sql = 'UPDATE eleve SET nom = :nom, prenom = :prenom, tel = :tel,  id_avatar = :id_avatar WHERE mail = :email';
				    $req = $connexion->prepare($sql);
				    $req->execute($u);
				      
				    $_SESSION['id_avatar'] = $id_avatar;
			      
			     	//var_dump(pathinfo($_FILES["file"]["name"]));
			     	//echo $_FILES["file"]["name"].'<br>';
					//var_dump($temp);
					header('Location:coordonneeseleve.php?mail='.$mail.'');
				      
					//echo "Stored in: " .  "images/avatar_eleve/" . $_FILES["file"]["name"];
				//}
			}
		}
		else
		{
			?>
			<script type='text/javascript'>
			alert("Le fichier n'est pas valide, veuillez choisir un .jpg");
			</script>
			<?php
		}
	}
	else
	{
		$nom = addslashes($_POST['nom']);
		$prenom = addslashes($_POST['prenom']);
		$tel =  addslashes($_POST['tel']);
		
		$u = array('nom'=>$nom,'prenom'=>$prenom, 'tel'=>$tel, 'email'=>$mail);
		$sql = 'UPDATE eleve SET nom = :nom, prenom = :prenom, tel = :tel WHERE mail = :email';
		$req = $connexion->prepare($sql);
		$req->execute($u);
		header('Location:coordonneeseleve.php?mail='.$mail.'');
	}
}
?>


<div class="text_coordonnees">

<label for="mail">Adresse mail : <?php echo $modifcoordonneeseleve->mail ?></label><br><br>

<form method="post" action="modifcoordonneeseleve.php?mail=<?php echo $mail ?>" enctype="multipart/form-data">
	<label for="nom">Nom : </label>
	<input type="text" name="nom" size="25" maxlength="50" value="<?php echo $modifcoordonneeseleve->nom ?>"/></span><br><br>
	<label for="prenom">Pr&eacute;nom : </label>
	<input type="text" name="prenom" size="25" maxlength="50" value="<?php echo $modifcoordonneeseleve->prenom ?>"/></span><br><br>
	<label for="tel">T&eacute;l&eacute;phone : </label>
	<input type="text" name="tel" size="25" maxlength="50" value="<?php echo $modifcoordonneeseleve->tel ?>"/></span><br><br>
	<label for="file">Uploader photo:</label>
	<input type="file" accept=".jpg" name="file" id="file"><br><br>
	<span class="submit_pad"><input type="submit" value="Valider les modifications"/></span>
</form>

</div>


<img  id='img' class='imageprincipal' src=<?php $_SERVER['HTTP_ORIGIN'] ?>/images/<?php echo $modifcoordonneeseleve->url1 ?>.jpg  alt=''/>



<?php 
$site->footer();